package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.pf.BusinessAnnual;
import com.plian.system.entity.pf.BusinessIndicators;
import com.plian.system.mapper.pf.BusinessAnnualMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.pf.IBusinessAnnualService;
import com.plian.system.service.pf.IBusinessIndicatorsService;
import com.plian.system.vo.pf.BusinessAnnualVO;
import com.plian.system.vo.pf.BusinessIndicatorsVO;
import com.plian.system.wrapper.pf.BusinessIndicatorsWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO 经营业绩考核年度 -财务绩效
 * @Date: 2022/12/18 12:07 PM
 * @Version: 1.0
 **/

@Slf4j
@Service
@AllArgsConstructor
public class BusinessAnnualServiceImpl extends BaseServiceImpl<BusinessAnnualMapper, BusinessAnnual> implements IBusinessAnnualService {

    private IBusinessIndicatorsService businessIndicatorsService;

    private BusinessIndicatorsWrapper businessIndicatorsWrapper;

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<BusinessAnnual> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BusinessAnnual::getBaseBusinessAnnualId, ids);
        List<BusinessAnnual> businessAnnuals = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(businessAnnuals)){
            return deleteLogic(businessAnnuals.stream().map(BusinessAnnual::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public void setDetailList(List<BusinessAnnualVO> businessAnnualVOS){
        List<BusinessIndicators> list = businessIndicatorsService.list(new LambdaQueryWrapper<BusinessIndicators>()
                .in(BusinessIndicators::getId, businessAnnualVOS.stream().map(BusinessAnnualVO::getBusinessIndicatorsId).collect(Collectors.toList())));
        if (CollectionUtil.isNotEmpty(list)){
            List<BusinessIndicatorsVO> businessIndicatorsVOS = businessIndicatorsWrapper.entityToVO(list);
            businessIndicatorsService.setBusinessName(businessIndicatorsVOS);
            //计算

            //放入
            businessAnnualVOS.forEach(businessAnnualVO -> {
                List<BusinessIndicatorsVO> businessIndicators = businessIndicatorsVOS.stream().filter(index -> businessAnnualVO.getBusinessIndicatorsId().equals(index.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(businessIndicators)){
                    BusinessIndicatorsVO vo = businessIndicators.get(0);
                    businessAnnualVO.setBusinessIndicatorsId(vo.getId());
                    businessAnnualVO.setIndicatorsCategoryId(vo.getIndicatorsCategoryId());
                    businessAnnualVO.setIndicatorsCategoryName(vo.getIndicatorsCategoryName());
                    if (!Optional.ofNullable(businessAnnualVO.getIndicatorsWeight()).isPresent()){
                        businessAnnualVO.setIndicatorsWeight(vo.getIndicatorsWeight());
                        businessAnnualVO.setBaseline(vo.getBaseline());
                        businessAnnualVO.setTarget(vo.getTarget());
                        businessAnnualVO.setBasicTarget(vo.getBasicTarget());
                    }
                }
            });
        }

    }


}
